package learn_bouncycastle.security;

import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.spec.InvalidParameterSpecException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class SecAES_ECB_Cipher {

	static final String CIPHER_NAME = "AES/ECB/PKCS5Padding";

	public static byte[] encrypt(byte[] key, byte[] input) throws NoSuchAlgorithmException, 
																	NoSuchPaddingException,
																	InvalidKeyException, 
																	IllegalBlockSizeException, 
																	BadPaddingException {
		Cipher cipher = Cipher.getInstance(CIPHER_NAME);
		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
		cipher.init(Cipher.ENCRYPT_MODE, keySpec);
		return cipher.doFinal(input);
	}
	
	public static byte[] decrypt(byte[] key, byte[] input) throws NoSuchAlgorithmException, 
																	NoSuchPaddingException, 
																	InvalidKeyException, 
																	IllegalBlockSizeException, 
																	BadPaddingException {
		Cipher cipher = Cipher.getInstance(CIPHER_NAME);
		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
		cipher.init(Cipher.DECRYPT_MODE, keySpec);
		return cipher.doFinal(input);
	}

	public static byte[] decrypt(byte[] key, byte[] iv, byte[] input) throws NoSuchAlgorithmException, 
																				NoSuchPaddingException, 
																				InvalidParameterSpecException, 
																				InvalidKeyException, 
																				InvalidAlgorithmParameterException, 
																				IllegalBlockSizeException, 
																				BadPaddingException, 
																				NoSuchProviderException {
		Security.addProvider(new BouncyCastleProvider());
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
		AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
		params.init(new IvParameterSpec(iv));
		cipher.init(Cipher.DECRYPT_MODE, keySpec, params);
		return cipher.doFinal(input);
	}
	
	
	
	public static void main(String[] args) throws Exception{
		String encryptedData = "a8mxwxCAFc2e1hWtu67jE2noMmpXYIQm6N8z+oZuU5m/3Z5lgcNo/CEwv1dn+HOjlD45o7ffRPWR6w5t1UNzB6GXBrOVuXBpC9LYu/zVsIwGRcXrg8/X393dqerzPrwZIc2L3s6rdhRw7jdpdBadMiwqr1I5kN/du2JXTtkjLuvwUU82xtpznuwU4rp0oXC7gGpZ7RWL8kQ0T11FQlN5BHXwWr/VyiUUbnU4YQKgrFkf3kdKXb14MQcppo9/SEO34EhgDKFzegfJBp7j1TV/1vtdey1mslIJbyBNr3u+LNrny+efwisB6QNe93d6SjHfcgDriLkv5sOPC3T8nBxKPbpNl0XUIKI1rjkESckoN+GRnXJUMGKq/aRhh3q29RSGH1Atg8HHP5n+4cqddVsTpQ3ADtte2qxWuSMPqtd4diGXfMGrbjgwo+ygQkNrKVGi/kCfsHfPHWcBAtyghDTJIg==";
		String iv = "YvgsZIpj5la76i+cOCGTbQ==";
		String sessionKey= "EVtybIPevBuS5KleOMNoyA==";
		
		String rawData = null;
		
		rawData = new String(decrypt(
									Base64.getDecoder().decode(sessionKey),
									Base64.getDecoder().decode(iv),
									Base64.getDecoder().decode(encryptedData) 
									), "utf8");
		
		System.out.println(rawData);
	}

}

























